//
//  SmallVideoConfig.m
//  ImagePicker
//
//  Created by YH on 2018/4/26.
//  Copyright © 2018年 王跃. All rights reserved.
//
#import <AVFoundation/AVFoundation.h>
#import "SmallVideoConfig.h"

@implementation SmallVideoModel

@end

@implementation SmallVideoConfig

//视频View的尺寸
+ (CGSize)videoViewDefaultSize
{
    return CGSizeMake(SmallVideoSCREEN_WIDTH, SmallVideoSCREEN_HEIGHT);
}

@end

@implementation SmallVideoUtils

+ (BOOL)existVideo
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *nameList = [fileManager subpathsAtPath:[self getVideoPath]];
    return nameList.count > 0;
}

+ (UIImage *)saveThumImageWithVideoURL:(NSURL *)videoUrl second:(int64_t)second
{
    NSLog(@"%@", videoUrl.absoluteString);
    AVURLAsset *urlSet = [AVURLAsset assetWithURL:videoUrl];
    AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlSet];

    CMTime time = CMTimeMake(second, 10);
    NSError *error = nil;
    CGImageRef cgimage = [imageGenerator copyCGImageAtTime:time actualTime:nil error:&error];
    if (error) {
        NSLog(@"缩略图获取失败!:%@",error);
        return nil;
    }
    UIImage *image = [UIImage imageWithCGImage:cgimage scale:0.6 orientation:UIImageOrientationRight];
    NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
    NSString *videoPath = [videoUrl.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString: @""];
    NSString *thumPath = [videoPath stringByReplacingOccurrencesOfString:@"mp4" withString: @"JPG"];
    BOOL isok = [imgData writeToFile:thumPath atomically: YES];
    NSLog(@"缩略图获取结果:%d",isok);
    CGImageRelease(cgimage);
    return image;
}

+ (SmallVideoModel *)createNewVideo
{
    [SmallVideoUtils initialize];
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    formate.dateFormat = @"yyyy-MM-dd_HH:mm:ss";
    NSString *videoName = [formate stringFromDate:currentDate];
    NSString *videoPath = [self getVideoPath];

    SmallVideoModel *model = [[SmallVideoModel alloc] init];
    model.videoAbsolutePath = [videoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",videoName]];
    model.thumAbsolutePath = [videoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.JPG",videoName]];
    model.recordTime = currentDate;
    return model;
}

+ (void)deleteVideo:(NSString *)videoPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error = nil;
    [fileManager removeItemAtPath:videoPath error:&error];
    if (error) {
        NSLog(@"删除视频失败:%@",error);
    }
    NSString *thumPath = [videoPath stringByReplacingOccurrencesOfString:@"mp4" withString:@"JPG"];
    NSError *error2 = nil;
    [fileManager removeItemAtPath:thumPath error:&error2];
    if (error2) {
        NSLog(@"删除缩略图失败:%@",error);
    }
}

+ (NSString *)getVideoPath
{
    return [self getDocumentSubPath:SmallVideoDicPathName];
}

+ (NSString *)getDocumentSubPath:(NSString *)dirName
{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
    NSLog(@"%@", documentPath);
    return [documentPath stringByAppendingPathComponent:dirName];
}

+ (void)initialize
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *dirPath = [self getVideoPath];

    NSError *error = nil;
    [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
    if (error) {
        NSLog(@"创建文件夹失败:%@",error);
    }
}

@end
